/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2019年1月7日
 */

package com.massyframework.beanskin.util.threadlocal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * {@link ThreadLocalBinderGroup} ,一组线程变量绑定器
 */
public final class ThreadLocalBinderGroup implements ThreadLocalBinder {

    private final Collection<ThreadLocalBinder> binders;

    /**
     * 构造方法
     * @param c {@link Collection},一组ThreadLocalBinder实例
     */
    public ThreadLocalBinderGroup(Collection<ThreadLocalBinder> c){
        this.binders = c == null ?
                Collections.emptyList() :
                    c;
    }

    /**
     * 将线程变量绑定到当前线程。
     */
    @Override
    public void bind() {
        for (ThreadLocalBinder binder: binders){
            binder.bind();
        }
    }

    /**
     * 撤销线程变量的绑定
     */
	@Override
	public void unbind() {
		List<ThreadLocalBinder> list = new ArrayList<>(this.binders);
		Collections.reverse(list);
		for (ThreadLocalBinder binder: list) {
			binder.unbind();
		}
	}
    
    
}
